package com.simon.jkxj.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.simon.jkxj.R;
import com.simon.jkxj.adapter.EncyclopediaNewsAdapter;
import com.simon.jkxj.model.EncyclopediaNews;

import java.util.Arrays;
import java.util.List;

public class EncyclopediaFragment extends Fragment {

    private RecyclerView recyclerView;
    private EditText searchEditText;
    private EncyclopediaNewsAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_encyclopedia, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 初始化控件
        recyclerView = view.findViewById(R.id.encyclopedia_list_view);
        searchEditText = view.findViewById(R.id.et_encyclopedia_search);
        
        // 设置RecyclerView
        setupRecyclerView();
        
        // 设置搜索功能
        setupSearch();
    }
    
    private void setupRecyclerView() {
        // 设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        
        // 创建并设置适配器
        List<EncyclopediaNews> newsList = Arrays.asList(EncyclopediaNews.getMockData());
        adapter = new EncyclopediaNewsAdapter(getContext(), newsList);
        recyclerView.setAdapter(adapter);
        
        // 设置点击事件
        adapter.setOnItemClickListener((news, position) -> {
            // 处理条目点击事件
            Toast.makeText(getContext(), "点击了: " + news.getTitle(), Toast.LENGTH_SHORT).show();
            // TODO: 打开文章详情页面
        });
    }
    
    private void setupSearch() {
        // 搜索框监听
        searchEditText.setOnEditorActionListener((v, actionId, event) -> {
            // 处理搜索操作
            String keyword = searchEditText.getText().toString().trim();
            if (!keyword.isEmpty()) {
                // 执行搜索
                Toast.makeText(getContext(), "搜索: " + keyword, Toast.LENGTH_SHORT).show();
                // TODO: 实现实际的搜索功能
            }
            return true;
        });
    }
} 